package com.cuixk.microserver.zookeeper.controller;

import com.cuixk.microserver.zookeeper.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/{id}")
    public User getUserInfo(@PathVariable Integer id) {
        User user = new User();
        user.setId(id);
        user.setName("provider");
        user.setAge(25);
        return user;
    }

    @Autowired
    private DiscoveryClient discoveryClient;

    @GetMapping("/instance")
    public String serviceUrl() {
        List<ServiceInstance> list = discoveryClient.getInstances("zookeeper-provider");
        if (list != null && list.size() > 0) {
            return list.get(0).getUri().toString();
        }
        return null;
    }
}
